#!/usr/bin/env python
# -*- coding:utf-8 -*-
import os
from time import sleep
import shutil
import requests

game_file = ["F:\Y11\game", "F:\Y11\game_2025_09_10\game",
             "F:\Y11\game_branch_temp\game", "F:\Y11\game_week_branch\game"]


def get_server_url(server_id):
    server_1 = [2001, 2002, 4001]
    server_2 = [2201, 2202, 2203]
    url = "http://192.168.6.243:8527/wiz/ctlSrv?"
    if int(server_id) in server_2:
        url = 'http://111.230.176.208:8627/wiz/ctlSrv?'
    elif int(server_id) in server_1:
        url = 'http://192.168.6.241:8627/wiz/ctlSrv?'
    return url


def server_update(server_id):
    result = requests.get(
        f"{get_server_url(server_id)}op=up.sh {server_id}").text
    result = result.replace("<br>", "\n").replace("<pre>", "").replace("</pre>", "")
    print(result)


def server_restart(server_id):
    result = requests.get(
        f"{get_server_url(server_id)}op=restart.sh {server_id}").text
    result = result.replace("<br>", "\n").replace("<pre>", "").replace("</pre>", "")
    print(result)


def server_revert(server_id):
    model = input("请选择重置模式:1、单文件重置 2、全部文件重置\n")
    if model == "1":
        autocode_name = input("请输入要重置的配置表名：")
        result = requests.get(
            f"{get_server_url(server_id)}op=revert.sh {server_id} {autocode_name}").text
        result = result.replace("<br>", "\n").replace("<pre>", "").replace("</pre>", "")
        print(result)
    elif model == "2":
        result = requests.get(
            f"{get_server_url(server_id)}op=revert.sh {server_id}").text
        result = result.replace("<br>", "\n").replace("<pre>", "").replace("</pre>", "")
        print(result)


def server_change_season(server_id):
    season = input("请输入赛季：\n")
    result = requests.get(
        f"{get_server_url(server_id)}op=swSeason.sh {server_id} {season}").text
    result = result.replace("<br>", "\n").replace("<pre>", "").replace("</pre>", "")
    print(result)


def server_autocode_view(server_id):
    result = requests.get(
        f"{get_server_url(server_id)}op=status.sh {server_id}").text
    result = result.replace("<br>", "\n").replace("<pre>", "").replace("</pre>", "")
    print(result)


def server_clean_up(server_id):
    result = requests.get(
        f"{get_server_url(server_id)}op=cleanup.sh {server_id}").text
    result = result.replace("<br>", "\n").replace("<pre>", "").replace("</pre>", "")
    print(result)


def client_revert():
    path = r'F:\Y11\game\game_Data\StreamingAssets\script\autocode'
    command = r'TortoiseProc.exe /command:revert /path:"{0}" /closeonend:2'.format(path)
    os.system(command)
    print("重置客户端配置表完成！")


def server_drop(server_id):
    result = requests.get(
        f"{get_server_url(server_id)}op=drop.sh {server_id}").text
    result = result.replace("<br>", "\n").replace("<pre>", "").replace("</pre>", "")
    print(result)
    for file in game_file:
        dat_path = os.path.join(file, r"game_Data\Data\dat\warmailn")
        if os.path.exists(dat_path):
            shutil.rmtree(dat_path)
            print(fr"删除{dat_path}")


def server_kill(server_id):
    result = requests.get(
        f"{get_server_url(server_id)}op=kill9.sh {server_id}").text
    result = result.replace("<br>", "\n").replace("<pre>", "").replace("</pre>", "")
    sleep(2)
    print(result)
    # result = requests.get(
    #     f"http://111.230.176.208:8627/wiz/ctlSrv?op=restart.sh {server_id}").text
    # result = result.replace("<br>", "\n").replace("<pre>", "").replace("</pre>", "")
    # print(result)


def server_switch(server_id):
    branch = input("请输入要转的分支：")
    result = ""
    if branch == "1" or branch == "":
        result = requests.get(
            f"{get_server_url(server_id)}op=switch.sh {server_id}").text
    else:
        result = requests.get(
            f"{get_server_url(server_id)}op=switch.sh {server_id} {branch}").text
    result = result.replace("<br>", "\n").replace("<pre>", "").replace("</pre>", "")
    print(result)
    for file in game_file:
        dat_path = os.path.join(file, r"game_Data\Data\dat\warmailn")
        if os.path.exists(dat_path):
            shutil.rmtree(dat_path)
            print(fr"删除{dat_path}")


def server_show(server_id):
    result = requests.get(
        f"{get_server_url(server_id)}op=proc.sh {server_id}").text
    result = result.replace("<br>", "\n").replace("<pre>", "").replace("</pre>", "")
    print(result)


def server_add_log(server_id):
    result = requests.get(
        f"{get_server_url(server_id)}op=openFightLog.sh {server_id}").text
    result = result.replace("<br>", "\n").replace("<pre>", "").replace("</pre>", "")
    print(result)


def server_account_ban_login(server_id):
    result = requests.get(
        f"{get_server_url(server_id)}op=lock.sh {server_id} you").text
    result = result.replace("<br>", "\n").replace("<pre>", "").replace("</pre>", "")
    print(result)


def server_run(server_id):
    result = requests.get(
        f"{get_server_url(server_id)}op=start.sh {server_id}").text
    result = result.replace("<br>", "\n").replace("<pre>", "").replace("</pre>", "")
    print(result)


def server_stop(server_id):
    result = requests.get(
        f"{get_server_url(server_id)}op=shut.sh {server_id}").text
    result = result.replace("<br>", "\n").replace("<pre>", "").replace("</pre>", "")
    print(result)


def server_engine(server_id):
    result = requests.get(
        f"{get_server_url(server_id)}op=enginesh.sh {server_id}").text
    result = result.replace("<br>", "\n").replace("<pre>", "").replace("</pre>", "")
    print(result)


def server_backend(server_id):
    season = input("请输入赛季：\n")
    result = requests.get(
        f"http://159.75.127.180:8527/wiz/switchSeason?srvId={server_id}&season={season}").text
    result = result.replace("<br>", "\n").replace("<pre>", "").replace("</pre>", "")
    print(result)
    for file in game_file:
        dat_path = os.path.join(file, r"game_Data\Data\dat\warmailn")
        if os.path.exists(dat_path):
            shutil.rmtree(dat_path)
            print(fr"删除{dat_path}")


def client_update(command):
    from websocket import create_connection, WebSocketTimeoutException
    import base64
    from urllib import parse
    import json
    import time
    ws = create_connection("ws://192.168.5.137:8092/rh", timeout=3)
    ws.send(json.dumps({"command": f"{command}"}))
    time.sleep(5)
    try:
        for i in range(100):
            result = base64.b64decode(parse.unquote(json.loads(ws.recv())['body'])).decode()
            print(result, end="")
            print("\n")
            if "job done" in result:
                break
    except WebSocketTimeoutException as e:
        pass
    ws.close()
